﻿namespace DepartmentSpecificStoreView
{
    using System.Collections.Generic;

    using Microsoft.SharePoint;

    public class BaseProjectBL
    {
        #region Fields

        private List<BaseProjectInfo> _StoreList;

        #endregion Fields

        #region Properties

        public List<BaseProjectInfo> StoreList
        {
            get
            {
                if ( _StoreList == null )
                    _StoreList = new List<BaseProjectInfo>( );
                return _StoreList;
            }
            set
            {
                _StoreList = value;
            }
        }

        #endregion Properties

        #region Methods

        public string GetDepartmentRecordID( SPWeb storeWeb, string projectID, string listName )
        {
            string result = "";

            storeWeb.AllowUnsafeUpdates = true;
            SPList departmentListValues = storeWeb.Lists[ listName ];

            AddField( "Store Reference ID", SPFieldType.Number, departmentListValues.Fields );

            SPQuery singleDepartmentDepartments = new SPQuery( );

            singleDepartmentDepartments.Query = "<Where><Eq><FieldRef Name='Store_x0020_Reference_x0020_ID' /><Value Type='Number'>" + projectID.Trim( ) + "</Value></Eq></Where>";
            singleDepartmentDepartments.ViewFields = "<FieldRef Name='ID'/>";
            SPListItemCollection departmentsID = departmentListValues.GetItems( singleDepartmentDepartments );
            if ( departmentsID.Count != 0 )
                result = departmentsID[ 0 ][ "ID" ].ToString( );
            else
                result = "";

            return result;
        }

        public void LoadAllStores( SPWeb storeWeb, string listName, bool activeOnly )
        {
            List<BaseProjectInfo> allStores = new List<BaseProjectInfo>( );
            List<DepartmentSecurityGroup> allSecGroups = new List<DepartmentSecurityGroup>( );
            BaseProjectInfo singleStore = new BaseProjectInfo( );
            SPQuery baseFieldsData = new SPQuery( );

            SPList departmentListValues = storeWeb.Lists[ "Stores Detail" ];
            baseFieldsData.ViewFields = "<FieldRef Name='ID'/><FieldRef Name='Reference_x0020__x0023_'/><FieldRef Name='Store_x0020_State'/><FieldRef Name='Title'/><FieldRef Name='Store_x0020_Security_x0020_Group'/>";
            if ( activeOnly )
                baseFieldsData.Query = "<Where><Eq><FieldRef Name='Store_x0020_Status' /><Value Type='Choice'>Active</Value></Eq></Where>";
            SPListItemCollection allBaseFields = departmentListValues.GetItems( baseFieldsData );

            SPList departmentList = storeWeb.Lists[ listName ];

            SecurityGroupBL secProc = new SecurityGroupBL( );
            secProc.LoadSelectedSecurityGroups( storeWeb, listName );

            allSecGroups = secProc.DepartmentSecurityGroups;

            foreach ( SPListItem item in allBaseFields )
            {
                singleStore = new BaseProjectInfo( );
                singleStore.ProjectID = item[ "ID" ].ToString( );
                singleStore.Listname = listName;
                singleStore.ListGUID = departmentList.ID.ToString( );

                if ( item[ "Reference #" ] != null )
                {
                    singleStore.Project = float.Parse( LookupStringClean( item[ "Reference #" ].ToString( ) ) ).ToString( );
                }
                if ( item[ "Store State" ] != null )
                    singleStore.State = item[ "Store State" ].ToString( );
                if ( item[ "Title" ] != null )
                    singleStore.Title = item[ "Title" ].ToString( );

                if ( item[ "Store Security Group" ] != null )
                {
                    foreach ( DepartmentSecurityGroup secItem in allSecGroups )
                    {
                        if ( LookupStringClean( item[ "Store Security Group" ].ToString( ).Trim( ) ).Equals( secItem.SecurityGroup ) )
                        {
                            allStores.Add( singleStore );
                        }
                    }
                }
            }
            StoreList = allStores;
        }

        public List<UIGridInfo> LoadBaseColumnWidth( SPWeb storeWeb )
        {
            SPList columnWidthListValues = storeWeb.Lists[ "BaseColumns" ];
            List<UIGridInfo> baseColumnWidth = new List<UIGridInfo>( );
            foreach ( SPListItem item in columnWidthListValues.Items )
                baseColumnWidth.Add( new UIGridInfo( int.Parse( item[ "Column Width" ].ToString( ) ), item[ "Title" ].ToString( ) ) );

            return baseColumnWidth;
        }

        private SPField AddField( string title, SPFieldType type, SPFieldCollection fields )
        {
            // If the field is not in the collection,
            if ( !fields.ContainsField( title ) )
            {
                // Add it.
                fields.Add( title, type, false );
            }
            return fields.GetField( title );
        }

        private string LookupStringClean( string input )
        {
            string result = "";
            if ( input.IndexOf( ";#" ) >= 0 )
                result = input.Substring( input.IndexOf( ";#" ) + 2 );
            else
                result = input;

            return result;
        }

        #endregion Methods
    }
}